package com.mall.exception;

import cn.hutool.core.convert.Convert;
import com.mall.utils.Result;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理器
 */
@RestControllerAdvice //注解配合@ExceptionHandler会将上抛至带有@RestController标注的类中的异常按类型进行处理
public class MyExceptionHandler {
    /**
     * 对ConsumerException进行统一处理
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ConsumerException.class)
    public Result consumerException(ConsumerException e) {
        //return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        //return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("发生未知错误！");
        return Result.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "发生未知错误！");
    }
}
